import { CommonUI } from "../../../[框架]/通用UI/CommonUI";
import TaskConfig, { ITaskConfig } from "../Model/TaskConfig";
import { TaskDefine } from "../Model/TaskDefine";
import { TaskManager } from "./TaskManager";

export default class TaskProxy {
    private static _inst: TaskProxy = null;

    private _taskConfig: TaskConfig = new TaskConfig();

    public static get inst(): TaskProxy {
        if (this._inst == null) {
            this._inst = new TaskProxy();
            this._inst.readCache();
        }
        return TaskProxy._inst;
    };

    private readCache() {
        let data = Guyu.storage.get("task_config", "");
        if (data != "") {
            try {
                let obj = JSON.parse(data);
                this.setData(JSON.parse(data));
                Guyu.Log.info(`任务配置缓存=>`, obj);
                // this._taskConfig.print();
            }
            catch (e) {
                Guyu.Log.error(`任务配置缓存读取失败`, e);
            }
        }
    }

    setData(taskConfig: ITaskConfig) {
        this._taskConfig = new TaskConfig().update(taskConfig);
        Guyu.dispatcher.event(TaskDefine.Events.Task_Update);
        // TaskManager.init();
        Guyu.storage.set("task_config", JSON.stringify(taskConfig));
        let config = this._taskConfig;
        let adUnitId1 = config.inset_adunits;
        let adUnitId2 = config.banner_adunits;
        if (Guyu.RuntimeSDK.onMiniGame) {
            Guyu.AdFactory.getInterstitialAd({ adUnitId: adUnitId1 });
            // Guyu.AdFactory.getCustomAd({ adUnitId: adUnitId2 });
        }
    }

    public get taskConfig(): TaskConfig {
        return this._taskConfig;
    }
}